package com.gang.service;

import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author: 李刚
 * @time: 2020/5/3 12:14
 * @description: 向队列添加数据
 */
@Component
@EnableScheduling
public class Producer {

    @Value("${topic.name}")
    private String topicName;

    @Value("${queue.name}")
    private String queueName;

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    /**
     * 功能描述: 发送到topic
     * @param: []
     * @return: void
     * @date: 2020/8/30 16:29
     */
//    @Scheduled(fixedDelay = 5000)
    public void sendToTopic() {
        jmsMessagingTemplate.convertAndSend(new ActiveMQTopic(topicName), "测试消息队列:" + System.currentTimeMillis());
    }

    /*
     * 功能描述: 发送到队列
     * @param: []
     * @date: 2020/8/30 16:29
     */
    @Scheduled(fixedDelay = 4)
    public void sendToQueue() {
        for (int i = 0; i <10000 ; i++) {

            jmsMessagingTemplate.convertAndSend(new ActiveMQQueue(queueName), "测试消息队列:" + System.currentTimeMillis());

        }

    }

}
